<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>题库分类设置</title>
    <link rel="stylesheet" href="/static/assets/libs/layui/css/layui.css" />
    <link rel="stylesheet" href="/static/assets/module/admin.css?v=312" />
    <style>
        .layui-form-item {
            margin-bottom: 25px;
        }

        label .layui-icon {
            font-size: 14px;
            color: #666;
        }
    </style>
</head>

<body>
    <!-- 正文开始 -->
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">

                <div class="layui-form toolbar">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <button id="btn_add" class="layui-btn icon-btn">
                                <i class="layui-icon">&#xe654;</i>添加分类
                            </button>
                            <!-- 下拉按钮 -->
                            <div class="dropdown-menu dropdown-hover">
                                <button class="layui-btn icon-btn">
                                    &nbsp;更多 <i class="layui-icon layui-icon-drop"></i></button>
                                <ul class="dropdown-menu-nav">
                                    <li><a id="btn_alldel"><i class="layui-icon layui-icon-delete"></i>全部清空</a></li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>

                <table class="layui-table" id="tableTbBas" lay-filter="tableTbBas"></table>
            </div>
        </div>
    </div>
    <!-- 表格操作列 -->
    <script type="text/html" id="tableBarTbBas">
    <a class="layui-btn layui-btn-sm" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-sm" lay-event="del">删除</a>
</script>
    <!-- js部分 -->
    <script type="text/javascript" src="/static/assets/libs/layui/layui.js"></script>
    <script type="text/javascript" src="/static/assets/js/common.js?v=312"></script>
    <script>
        layui.use(['layer', 'jquery', 'table', 'dropdown'], function () {
            var $ = layui.jquery;
            var layer = layui.layer;
            var table = layui.table;
            var dropdown = layui.dropdown;
            // 渲染表格
            var insTb = table.render({
                elem: '#tableTbBas',
                url: "{:url('admin/main/sort',['type'=>'get'])}",
                page: true,
                cols: [[
                    { field: 'listorder', align: 'center', sort: true, title: '排序', width: 90, edit: 'text' },
                    { field: 'Id', align: 'center', sort: true, title: 'ID', width: 90 },
                    { field: 'name', align: 'center', title: '分类名称' },
                    { field: 'alias', align: 'center', title: '分类别名', width: 130 },
                    { field: 'key', align: 'center', title: '分类关键词' },
                    { field: 'desc', align: 'center', title: '分类介绍' },
                    {
                        field: 'fid', align: 'center', title: '上级', width: 80, templet: function (d) {
                            if (d.fid == 0) {
                                return '<span class="layui-badge">顶</span>';
                            } else {
                                return d.fid;
                            }
                        }
                    },
                    { align: 'center', toolbar: '#tableBarTbBas', title: '操作', width: 150 }
                ]]
            });
            //编辑排序监听
            table.on('edit(tableTbBas)', function (obj) {
                var value = obj.value,
                    data = obj.data;
                $.post('{:url("admin/main/sort")}', { 'id': data.Id, 'value': value, 'type': 'listorder' }, function (data) {
                    if (data.code == 1) {
                        layer.msg(data.msg);
                    } else {
                        layer.msg(data.msg);
                    }
                })
            });
            //监听工具条
            table.on('tool(tableTbBas)', function (obj) {
                var data = obj.data;
                var layEvent = obj.event;
                if (layEvent === 'edit') {
                    layer.open({
                        type: 2,
                        area: '500px',
                        title: '修改分类',
                        content: "{:url('admin/main/sort_edit')}?id=" + data.Id,
                        end: function () {
                            insTb.reload();
                        }
                    });
                } else if (layEvent === 'del') { // 删除
                    layer.open({
                        title: '删除确认'
                        , content: '将要分类：' + data.name + ' 删除吗？'
                        , btn: ['确认', '取消']
                        , yes: function (index, layero) {
                            $.ajax({
                                type: "POST",
                                url: "{:url('admin/main/sort')}",
                                data: 'type=del&id=' + data.Id,
                                dataType: "json",
                                success: function (data) {
                                    if (data.code == 0) {
                                        layer.msg('删除成功', {
                                            icon: 1,
                                            time: 2000
                                        }, function () {
                                            obj.del();
                                        });
                                    } else if (data.code == -1) {
                                        layer.msg('删除失败');
                                    }
                                },
                                error: function (data) {
                                    alert("请求失败");
                                }
                            });
                            layer.closeAll();
                        }
                    });

                }
                dropdown.hideAll();
            });

            $('#btn_add').click(function () {
                layer.open({
                    type: 2,
                    area: '500px',
                    title: '添加分类',
                    content: "{:url('admin/main/sort_add')}",
                    end: function () {
                        insTb.reload();
                    }
                });
            });
            $('#btn_alldel').click(function () {
                layer.open({
                    title: '删除确认'
                    , content: '将要删除全部分类内容！确定吗？'
                    , btn: ['确认', '取消']
                    , yes: function (index, layero) {
                        $.ajax({
                            type: "POST",                      //请求类型
                            url: "{:url('admin/main/sort')}",//URL
                            data: 'type=delall',   //传递的参数
                            dataType: "json",                 //返回的数据类型
                            success: function (data) {
                                if (data.code == 0) {
                                    layer.alert('清空成功！');
                                    insTb.reload();
                                } else if (data.code == -1) {
                                    layer.alert('清空失败');
                                }
                            },
                            error: function (data) {
                                alert("请求失败");
                            }
                        });
                        layer.closeAll();
                    }
                });
            });

        });
    </script>
</body>

</html>